package com.gxa.controller;


import com.gxa.pojo.Project;

import com.gxa.pojo.ProjectV;
import com.gxa.pojo.ProjectVO;
import com.gxa.service.impl.ProjectServiceImpl;
import com.gxa.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "项目管理操作")
@RequestMapping("Project")
@Repository
public class ProjectController {

    @Autowired
    ProjectServiceImpl projectService;
    /**
     *
     *
     * @return 项目数量和所有项目的数据
     */
    @ApiOperation(value = "显示分页数据",httpMethod ="GET",response = ResponseData.class)
    @GetMapping("/showAllProject")
    @ResponseBody
    public ResponseData showAllProject(ProjectV projectV){

        ResponseData responseData=new ResponseData();
        List<Project> projects= projectService.showAllProject(projectV);
        responseData.setCount(projectService.getCount());
        responseData.setData(projects);
        return responseData;
    }

    /**
     *条件查询
     * @param projectName 项目名称
     * @param projectNumber 项目编号
     * @param projectStatus 项目状态
     * @param page
     * @param limit
     * @return 满足条件的项目数据
     */

    @ApiResponse(code = 200,message = "响应成功",response = ResponseData.class)
    @ApiOperation(value = "按条件查询项目数据",httpMethod ="GET",response = String.class)
    @GetMapping("/selectByCondition")
    @ResponseBody
    public ResponseData selectByCondition(ProjectV projectV){

        ResponseData responseData=new ResponseData();
        List<Project> projects=projectService.selectByCondition(projectV);
        for(Project project:projects){
            System.out.println(project);
        }
        responseData.setCount(projectService.getCount());
        responseData.setData(projects);
        return responseData;
    }


    /**
     *
     * @param project
     * @return 是否添加成功
     */

    @ApiOperation(value = "添加项目数据",httpMethod ="GET",response = ResponseData.class)
    @GetMapping("/addProject")
    @ResponseBody
    public ResponseData addProject(@ApiParam(value = "一个project对象，必须有项目名称和项目距编号的属性",required = true)Project project){
        ResponseData responseData=new ResponseData();
        Boolean result=projectService.addProject(project);
        responseData.setData(result);
        return responseData;
    }

    /**
     *单个删除
     * @param projectNumber 项目编号
     * @return 删除结果:true/false
     */

    @ApiOperation(value = "单个删除数据",httpMethod ="GET",response = ResponseData.class)
    @GetMapping("/delectByProjectNumber")
    @ResponseBody
    public ResponseData delectByProjectNumber(@ApiParam(value = "项目编号")String projectNumber){
        Boolean result=projectService.deleteByProjectNumber(projectNumber);
        ResponseData responseData=new ResponseData();
        responseData.setData(result);
        return responseData;
    }

    /**
     * 批量删除
     * @param projectNumbers 需要删除的项目编号列表
     * @return 删除的项目个数
     */

    @ApiOperation(value = "根据项目编号的数组删除数据",httpMethod ="POST",response = ResponseData.class)
    @PostMapping("/deleteByProjectNumbers")
    @ResponseBody
    public ResponseData deleteByProjectNumbers(@RequestParam List<String> projectNumbers){
        //计算删除的个数
        int count=0;
        for(String projectNumber:projectNumbers){
            Boolean result=projectService.deleteByProjectNumber(projectNumber);
            if(result==true){
                count++;
            }
        }
        ResponseData responseData=new ResponseData();
        responseData.setData(count);
        return responseData;
    }


    /**
     *根据项目编号查找数据
     * @param projectNumber 项目编号
     * @return 查询结果
     */

    @ApiOperation(value = "根据项目编号查找数据",httpMethod ="GET",response = ResponseData.class)
    @GetMapping("/selectByProjectNumber")
    @ResponseBody
    public ResponseData selectByProjectNumber(@ApiParam(value = "项目编号")String projectNumber){
        ResponseData responseData=new ResponseData();
        Project project=projectService.selectByProjectNumber(projectNumber);
        responseData.setData(project);
        return responseData;
    }

}
